package org.kjtc.balanceController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.controller.BaseController;
import org.kjtc.entity.UserInfo;
import org.kjtc.service.UserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ReviewJurisdictionController extends BaseController{
	
	@Resource(name = "userInfoService")
	private UserInfoService userInfoService;
	
	@RequestMapping(value = "/searchReviewJurisdiction")
	@ResponseBody
	public Map<String, Object> searchUserTeam(UserInfo userInfo){

		Map<String, Object> map = new HashMap<String, Object>();
		List<UserInfo> list = null;
		try {
			list = userInfoService.getUserTeam(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "车队用户信息查询异常" + e.getMessage());
			return map;
		}
		map.put("userInfoList", list);
		list = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/saveReviewJurisdiction")
	public Map<String, Object> saveReviewJurisdiction(UserInfo userInfo){
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		String[] teamIDList = userInfo.getTeamID().split(",");
		String userID = userInfo.getUserID();
		String subBranchCompanyID = userInfo.getSubBranchCompanyID();
		for(int i = 0; i < teamIDList.length; i++) {
			UserInfo userInfos = new UserInfo();
			userInfos.setSubBranchCompanyID(subBranchCompanyID);
			userInfos.setUserID(userID);
			userInfos.setTeamID(teamIDList[i]);
			userInfoList.add(userInfos);
			userInfos = null;
		}
		int count = 0;
		try {
			count = userInfoService.saveUserTeam(userInfoList);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
		userInfo = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateReviewJurisdiction")
	public Map<String, Object> updateReviewJurisdiction(UserInfo userInfo){
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		String[] teamIDList = userInfo.getTeamID().split(",");
		String userID = userInfo.getUserID();
		String subBranchCompanyID = userInfo.getSubBranchCompanyID();
		for(int i = 0; i < teamIDList.length; i++) {
			UserInfo userInfos = new UserInfo();
			userInfos.setSubBranchCompanyID(subBranchCompanyID);
			userInfos.setUserID(userID);
			userInfos.setTeamID(teamIDList[i]);
			userInfoList.add(userInfos);
			userInfos = null;
		}
		
		
		int count1 = 0;
		int count2 = 0;
		try {
			count2 = userInfoService.deleteUserTeam(userInfo);
			count1 = userInfoService.saveUserTeam(userInfoList);
			if (count1 > 0 && count2 > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
		userInfo = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteReviewJurisdiction")
	public Map<String, Object> deleteReviewJurisdiction(UserInfo userInfo){
		
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		try {
			count = userInfoService.deleteUserTeam(userInfo);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加审核记录异常:" + e.getMessage());
		}
		userInfo = null;
		return map;
	}
}